/*
 * Copyright © 2017-2023 TemplateFactory(xiaoymin@foxmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.github.xiaoymin.template.render.aide;

import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.template.config.TemplateConfiguration;
import com.github.xiaoymin.template.core.TemplateContext;
import com.github.xiaoymin.template.core.TemplateDataSource;
import com.jfinal.template.Template;
import com.jfinal.template.source.ISource;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/***
 *
 * @since template-factory 1.0
 * @author <a href="mailto:xiaoymin@foxmail.com">xiaoymin@foxmail.com</a> 
 * 2020/06/07 17:28
 */
public class AideStoreControllerTemplateRender extends AbstractTemplateRender {
    
    @Override
    public Map<String, Object> buildKeys(TemplateContext templateContext, TemplateConfiguration templateConfiguration) {
        Map<String, Object> map = new HashMap<>();
        map.put("packageName", templateConfiguration.getPackageName());
        map.put("modelName", templateConfiguration.getDomainObjectName());
        map.put("modelArgumentName", StrUtil.lowerFirst(templateConfiguration.getDomainObjectName()));
        // 模块
        map.put("module", templateConfiguration.getProperty("module"));
        map.put("api", templateConfiguration.getProperty("api"));
        String description = "接口API";
        String infodes = templateConfiguration.getProperty("description");
        if (StrUtil.isNotBlank(infodes)) {
            description = infodes;
        }
        // 开发者信息
        map.put("email", templateContext.getConfiguration().getDeveloperConfiguration().getEmail());
        map.put("projectName", templateContext.getConfiguration().getMavenProject().getName());
        map.put("projectVersion", templateContext.getConfiguration().getMavenProject().getVersion());
        
        map.put("description", description);
        map.put("currentTime", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm")));
        return map;
    }
    
    @Override
    public String buildTargetPath(TemplateContext templateContext, TemplateConfiguration templateConfiguration) {
        return getDefaultPathByType(templateContext, templateConfiguration, "web", templateConfiguration.getDomainObjectName() + "Controller.java");
    }
    
    @Override
    public void render(TemplateContext templateContext, TemplateConfiguration templateConfiguration) {
        ISource source = new TemplateDataSource(true, templateConfiguration.getPath());
        Template template = getEngine().getTemplate(source);
        template.render(this.buildKeys(templateContext, templateConfiguration), buildTargetPath(templateContext, templateConfiguration));
    }
}
